<template>
  <div class="app-container">
    <!--设备管理列表查询条件-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="序号" prop="orderNum">
          <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入序号模糊查询"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceNum">
          <el-input
            v-model="queryParams.deviceNum"
            placeholder="请输入设备编号模糊查询"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="queryParams.sysNormalDisable" style="width: 250px" placeholder="请选择状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="wmsDictDeviceType">
          <el-select v-model="queryParams.wmsDictDeviceType" style="width: 250px" filterable clearable placeholder="请选择设备类型" clearable size="small">
            <el-option
              v-for="dict in dict.type.wms_dict_device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModel">
          <el-input
            v-model="queryParams.deviceModel"
            placeholder="请输入设备型号模糊查询"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称模糊查询"
            clearable
            size="small"
            @keyup.enter.native="handleQuery" style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="负责人" prop="masterName">
          <el-select
            filterable
            v-model="queryParams.masterName"
            placeholder="请选择负责人"
            clearable
            size="small"
            style="width: 250px"
          >
            <el-option
              v-for="item in masterList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.nickName"
            />
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>
    <!--批量操作按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['comn:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['comn:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['comn:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['comn:device:export']"
        >导出</el-button>
      </el-col>
      <!--列表右侧上方小控件-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--设备管理列表展示-->
    <el-table v-loading="loading" stripe :data="deviceList" @selection-change="handleSelectionChange" :highlight-current-row="true">
      <el-table-column type="selection" width="50" align="center" fixed="left"/>
      <el-table-column type="expand" width="80">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="备注：">
              <span>{{ props.row.docNote }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="序号" align="center" prop="orderNum" width="80"/>
      <el-table-column label="设备类型" align="center" prop="wmsDictDeviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_dict_device_type" :value="scope.row.wmsDictDeviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备编号" align="center" prop="deviceNum" />
      <el-table-column label="设备型号" align="center" prop="deviceModel" />
      <el-table-column label="负责人" align="center" prop="masterName" />
      <el-table-column label="状态" align="center" prop="sysNormalDisable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.sysNormalDisable"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['comn:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['comn:device:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页查询-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal=false>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item prop="orderNum">
                    <span slot="label">
                      序号
                     <el-tooltip content="序号可留空，建议将常用客户的序号设置的小一些，以便将其排在前面。" placement="top">
                      <i class="el-icon-question"></i>
                      </el-tooltip>
                    </span>
          <el-input v-model="form.orderNum" clearable placeholder="请输入序号" :style="{width: '100%'}"/>
        </el-form-item>

        <el-form-item label="设备类型" prop="wmsDictDeviceType">
          <el-select v-model="form.wmsDictDeviceType" clearable filterable  placeholder="请选择设备类型(字典表)" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.wms_dict_device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" clearable :style="{width: '100%'}"/>
        </el-form-item>

        <el-form-item label="设备编号" prop="deviceNum">
          <el-input v-model="form.deviceNum" placeholder="请输入设备编号" clearable :style="{width: '100%'}"/>
        </el-form-item>

        <el-form-item label="设备型号" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入设备型号" clearable :style="{width: '100%'}"/>
        </el-form-item>

        <el-form-item label="负责人" prop="fkUserIdMaster">
          <el-input v-model="form.fkUserIdMaster"
                    v-show="false"
                    :style="{width: '100%'}"
          />
          <el-input placeholder="请点击右侧按钮选择负责人" readOnly="true"  v-model="form.masterName" class="input-with-select" >
            <el-button type="info" plain @click="dialog_show_selectUser" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="form.sysNormalDisable" placeholder="请选择状态" clearable filterable :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="docNote">
          <el-input v-model="form.docNote" placeholder="请输入备注" :style="{width: '100%'}" type="textarea" autosize/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--组件-->
    <select-user ref="component_selectUser" :roleId="queryParams.roleId" @ok="selectUser_done"/>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/comn/device";
import selectUser from "../../system/user/selectUser";
import { listUser} from "@/api/system/user";
export default {
  name: "Device",
  dicts: ['wms_dict_device_type', 'sys_normal_disable'],
  components: { selectUser },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      //负责人列表
      masterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        wmsDictDeviceType: null,
        deviceName: null,
        deviceNum: null,
        deviceModel: null,
        fkUserIdMaster: null,
        sysNormalDisable: null,
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        confirmDt: null,
        oaDictConfirmResult: null,
        confirmNote: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wmsDictDeviceType: [
          { required: true, message: "设备类型(字典表)不能为空", trigger: "change" }
        ],
        fkUserIdMaster: [
          { required: true, message: "负责人(关联用户表)不能为空", trigger: "change" }
        ],
        sysNormalDisable: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        oaDictConfirmResult: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getList_masterName();
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**获取负责人列表*/
    getList_masterName(){
      listUser().then(response => {
        this.masterList = response.rows
      });
    },
    /**取消按钮*/
    cancel() {
      this.open = false;
      this.reset();
    },
    /**表单重置*/
    reset() {
      this.form = {
        deviceUuid: null,
        orderNum: null,
        wmsDictDeviceType: null,
        deviceName: null,
        deviceNum: null,
        deviceModel: null,
        fkUserIdMaster: null,
        masterName: null,
        sysNormalDisable: '0',
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        confirmDt: null,
        oaDictConfirmResult: null,
        confirmNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作(清空查询条件进行查询) */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /**多选框选中数据*/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceUuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 设备列表数据新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },

    /** 设备列表数据修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceUuid = row.deviceUuid || this.ids
      getDevice(deviceUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },

    /** 修改或新增表单提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceUuid != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 设备列表数据删除按钮操作 */
    handleDelete(row) {
      const deviceUuids = row.deviceUuid || this.ids;
      this.$modal.confirm('是否确认删除设备名称为"' + row.deviceName + '",设备编号为"' + row.deviceNum + '"的数据项？','删除确认').then(function() {
        return delDevice(deviceUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('comn/device/export', {
        ...this.queryParams
      }, `comn_device.xlsx`)
    },

    /** 打开授权用户表弹窗 */
    dialog_show_selectUser() {
      this.$refs.component_selectUser.show();
    },

    /** 选择用户完成 */
    selectUser_done(user) {
      this.form.fkUserIdMaster = user.userId
      this.form.masterName = `${user.nickName}（用户名：${user.userName}，ID：${user.userId}）`
    },
  }
};
</script>
